package org.freespace.controller;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.freespace.model.Testplan;
import org.freespace.service.ITestplanService;
import org.freespace.utils.SessionUtil;
import org.freespace.utils.response.CommonResult;
import org.freespace.vo.TestPlanVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * <p>
 * 测试计划表 前端控制器
 * </p>
 *
 * @author liuqi
 * @since 2023-04-27
 */
@Slf4j
@RestController
@RequestMapping("/testplan")
public class TestplanController {

    @Autowired
    ITestplanService testplanService;

    @PostMapping(value = "list")
    public CommonResult list(@RequestBody TestPlanVO vo) {
        IPage<Testplan> page = new Page<>(vo.getCurrentPage(), vo.getPageSize());
        Wrapper<Testplan> wrapper = Wrappers.query();
        if(StringUtils.isNotBlank(vo.getCondition())) {
            wrapper = Wrappers.<Testplan>lambdaQuery()
                    .like(Testplan::getId, vo.getCondition())
                    .or().like(Testplan::getName, vo.getCondition())
                    .or().like(Testplan::getCreateBy, vo.getCondition());
        }
        IPage<Testplan> data = testplanService.page(page, wrapper);
        log.info("{}", data);
        return CommonResult.success(data);
    }

    @GetMapping(value = "get/{testplanId}")
    public CommonResult get(@PathVariable("testplanId") Integer testplanId) {
        return CommonResult.success(testplanService.getById(testplanId));
    }

    @GetMapping(value = "delete/{testplanId}")
    public CommonResult del(@PathVariable("testplanId") Integer testplanId) {
        return CommonResult.success(testplanService.removeById(testplanId));
    }

    @PostMapping(value = "save")
    public CommonResult save(@RequestBody Testplan testplan) {
        testplan.setCreateBy(SessionUtil.getCurrentUser());
        testplan.setCreateTime(LocalDateTime.now());
        testplan.setLastUpdateBy(SessionUtil.getCurrentUser());
        testplan.setLastUpdateTime(LocalDateTime.now());
        testplanService.save(testplan);
        return CommonResult.success();
    }
}
